﻿using SharpDepend.Datatypes;
using System;
using System.Collections.Generic;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.GUI.Controls;
using ZquenceStudio3Host.Graphics;
using ZquenceStudio3Host.Graphics.Components;
using ZquenceStudio3Host.Graphics.EntityProperties;
using ZquenceStudio3Host.Graphics.GUI;

namespace DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Editors
{
    class PropertiesEditor : Listbox
    {
        private class PropertyValueEditor : ValueEditor
        {
            private EntityProperty mProperty;
            private PropertiesEditor mEditor;

            public PropertyValueEditor(BasicGuiManager engineCore, EntityProperty property, PropertiesEditor editor)
                : base(engineCore, property.Name)
            {
                mProperty = property;
                mEditor = editor;
                Text = property.ToString();
            }

            protected override void TextChanged(string newText)
            {
                try
                {
                    mProperty.Parse(newText);
                    BackgroundColor = Color.Transparent;

                    mEditor.InvokePropertyChanged();
                }
                catch (Exception)
                {
                    BackgroundColor = Color.Red;
                }
            }
        }

        private HashSet<string> mIgnoreProperties = new HashSet<string>();

        private BaseObject mEditablePanel;

        public event Action PropertyChanged;

        internal void InvokePropertyChanged()
        {
            PropertyChanged?.Invoke();
        }

        public PropertiesEditor(BasicGuiManager engineCore) : base(engineCore)
        {
            mIgnoreProperties.Add(BaseObject.PropertyRotation);
            mIgnoreProperties.Add(BaseObject.PropertyTagName);
            mIgnoreProperties.Add(BaseObject.PropertyVisible);
            mIgnoreProperties.Add(Panel.PropertyMaxSize);
            mIgnoreProperties.Add(Panel.PropertyMinSize);
            mIgnoreProperties.Add(Panel.PropertyOverlayTexture);
            mIgnoreProperties.Add(Panel.PropertyOverlayTextureColor);
            mIgnoreProperties.Add(Panel.PropertyTexture);
        }

        public BaseObject EditablePanel
        {
            get { return mEditablePanel; }
            set
            {
                mEditablePanel = value;

                base.Clear();

                // Get all properties from the panel.
                string[] propertyNames = DefinedProperties.GetAllPropertyDefinisions(value);
                foreach (string propertyName in propertyNames)
                {
                    EntityProperty property = value.GetProperty(propertyName);

                    if (!mIgnoreProperties.Contains(propertyName))
                    {
                        // Create editable panels for every property.
                        PropertyValueEditor valueEditor = new PropertyValueEditor(EngineCore, property, this);
                        valueEditor.Width = 1000;
                        valueEditor.Height = 50;
                        base.AddItem(new ListboxItem(EngineCore, valueEditor, property));
                    }
                }
            }
        }
    }
}
